<!DOCTYPE html>
<html class="ui-page-login">
<head>
    <meta charset="utf-8"/>
    <meta name="viewport"
          content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no"/>
    <title></title>
    <script src="__PUBLIC__/js/jquery.min.js"></script>
    <script src="__PUBLIC__/home/plugins/layer/layer.js"></script>
    <script src="__PUBLIC__/mui/js/mui.min.js"></script>
    <script src="__PUBLIC__/js/angular.js"></script>
    <script src="__PUBLIC__/common.js"></script>
    <link rel="stylesheet" href="__PUBLIC__/mui/css/mui.css"/>
    <link rel="stylesheet" href="__PUBLIC__/js/nice-validator-0.10.11/dist/jquery.validator.css"/>
    <script src="__PUBLIC__/js/nice-validator-0.10.11/dist/jquery.validator.js"></script>
    <script src="__PUBLIC__/js/nice-validator-0.10.11/dist/local/zh-CN.js"></script>
    <style>
        .btn-width {
            margin-right: 1em;
            text-align: center;
            padding: 5px 10px !important;
            height: 30px;
            margin-top: 5px;
            border-radius: 0;
            width: 20% !important;
        }

        #member-login {
            display: none;
        }

        .area {
            margin: 20px auto 0px auto;
        }

        .mui-input-group {
            margin-top: 10px;
        }

        .mui-input-group:first-child {
            margin-top: 20px;
        }

        .mui-input-group label {
            width: 50%;
            font-size: 15px;
            color: black;

        }

        .mui-input-row label ~ input,
        .mui-input-row label ~ select,
        .mui-input-row label ~ textarea {
            width: 70%;
        }

        .mui-checkbox input[type=checkbox],
        .mui-radio input[type=radio] {
            top: 6px;
        }

        .mui-content-padded {
            margin-top: 25px;
        }

        .mui-btn {
            padding: 10px;
        }

        .link-area {
            display: block;
            margin-top: 25px;
            text-align: center;
        }

        .spliter {
            color: #bbb;
            padding: 0px 8px;
        }

        .oauth-area {
            position: absolute;
            bottom: 20px;
            left: 0px;
            text-align: center;
            width: 100%;
            padding: 0px;
            margin: 0px;
        }

        .oauth-area .oauth-btn {
            display: inline-block;
            width: 50px;
            height: 50px;
            background-size: 30px 30px;
            background-position: center center;
            background-repeat: no-repeat;
            margin: 0px 20px;
            /*-webkit-filter: grayscale(100%); */
            border: solid 1px #ddd;
            border-radius: 25px;
        }

        .oauth-area .oauth-btn:active {
            border: solid 1px #aaa;
        }

        .oauth-area .oauth-btn.disabled {
            background-color: #ddd;
        }

        .login-active {
            background: #f3363a;
        }

        .content {
            width: 50%;
            float: right;
            line-height: 40px;
            height: 40px;
            text-indent: 2em;
            font-size: 16px;
            text-align: right;
            padding-right: 1em;

        }

        .go-item {
            position: absolute;
            bottom: 0;
            left: 0;
            right: 0;
            padding: 0;
            margin: 0;
            background: white;
            height: 60px;
            text-align: center;

        }

        #user-login .mui-input-row .time {
            width: 65%;
            color: black;
        }

        #user-login .mui-input-row .status {
            width: 35%;
            font-size: 14px;
            font-weight: bold;
            font-family: "Microsoft YaHei UI Light";

        }

        .mui-input-group .mui-input-row:nth-child(1) {
            border-top: 1px solid #c8c7cc;
        }

        .mui-input-group .mui-input-row:after {
            left: 0;
        }

        .mui-input-group .mui-input-row:nth-child(1):after {
            background: none;
        }

        .mui-input-group .mui-input-row:last-child:after {
            background: none;
        }

        #user-login .mui-input-group {
            margin-top: 0;
        }

        #withdraw {
            color: white;
            font-size: 16px;
            line-height: 44px;
            margin: 0;
            padding: 0;
            height: 44px;
        }

        #user-login .mui-input-group {
            background: #f6f6f6
        }

    </style>

</head>
<body ng-app="myApp" ng-controller="Ctrl" ng-cloak>
<header class="mui-bar mui-bar-nav" style="background:#f3363a;">
    <a class="mui-icon mui-icon-left-nav mui-pull-left" style="color: white" href="/index.php/Home/agen/item"></a>
    <h1 class="mui-title login-item" style="color: white">去下单</h1>
</header>


<div class="mui-content">

    <div class="mui-input-group" style="margin-top: 3px" ng-repeat="pro in products">
        <div class="mui-input-row" style="background:white">
            <label class="time">{{pro.name}}</label>
            <div class="content  " style="color: #f2373b">
                <div class="mui-media-body">
                    <div class="mui-numbox mui-pull-right" data-numbox-min="0" style="margin: 6px 0px;width: 136px;height: 25px">
                        <button class="mui-btn mui-btn-numbox-minus" data-numbox-min="0" type="button" ng-click="pro.num = pro.num -1;jisuan(pro.id)">- </button>
                        <input class="mui-input-numbox" type="number" string-to-number ng-model="pro.num" ng-change="jisuan(pro.id)" />
                        <button class="mui-btn mui-btn-numbox-plus"  data-numbox-min="0" type="button" ng-click="pro.num = pro.num + 1;jisuan(pro.id)">+ </button>
                    </div>
                </div>
            </div>
        </div>

        <div class="mui-input-row" style="background:white">
            <label class="time"><span style="float: right">库存<span>{{pro.snum}}</span>盒</span></label>
            <button type="button" class="go-deal mui-btn mui-btn-danger mui-btn-outlined" ng-show="pro.num>0" style=" margin-right: 10px;" ng-click="sub(pro.id,pro.num)">下单</button>
            <button type="button" class="go-deal mui-btn mui-btn-danger mui-btn-outlined" ng-show="pro.num<=0" style=" margin-right: 10px;" disabled>下单</button>
        </div>
    </div>

</div>
<include file="./Application/Home/View/pub/agentfoot.html"/>
<script>
    var app = angular.module("myApp", []);

    //string转int
    app.directive('stringToNumber', function() {
        return {
            require: 'ngModel',
            link: function(scope, element, attrs, ngModel) {
                ngModel.$parsers.push(function(value) {
                    return '' + value;
                });
                ngModel.$formatters.push(function(value) {
                    return parseInt(value);
                });
            }
        };
    })

    app.controller('Ctrl', function ($scope, $http) {
        $http({
            method: 'GET',
            url: "/index.php/Home/AgentOnlineorder/getStockAndProductInfo"
        }).then(function successCallback(response) {
            //console.log(response.data.data);
            $scope.products = response.data.data;
            $scope.floor = function (number) {
                return Math.floor(number);
            };
        }, function errorCallback(response) {
        });

        //改变计算
        $scope.jisuan = function (id) {
            for (var x in $scope.products) {
                $scope.products[x].num = Math.ceil($scope.products[x].num);
                if($scope.products[x].id == id){
                    if ($scope.products[x].num <= 0) {
                        $scope.products[x].num = 0;
                        $scope.products[x].prozong = 0;
                    } else if ($scope.products[x].snum - $scope.products[x].num < 0) {
                        $scope.products[x].num = $scope.products[x].snum;
                        $scope.products[x].prozong = $scope.products[x].snum * $scope.products[x].myprice;
                        layer.msg("此商品库存不足，请先补充库存",{time:1000})
                    } else {
                        $scope.products[x].prozong = $scope.products[x].num * $scope.products[x].myprice;
                    }
                }
            }
        }

        //下单
        $scope.sub = function (id,num) {
            layer.confirm('确认提交？', {
                btn: ['取消','确认'] //按钮
            }, function(){
                layer.msg('已取消');
            }, function() {
                window.location.href = "/index.php/Home/Agen/comorder?id="+id+"&num="+num;
            })
        }
    })
</script>
</body>
</html>